﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using OfficialWeb.Application;

using OOS.Application.Address.Services.Interfaces;
using OOS.Application.Order.Dtos;
using OOS.Application.Order.Services.Interfaces;
using OOS.Application.Order.Vos;
using OOS.Application.User.Services.Interfaces;
using OOS.Core.Order;
using OOS.Core.RelyOn;

namespace OOS.Application.Order.Services
{
    /// <summary>
    /// 订单管理
    /// </summary>
    public class OrderBasicService: IOrderBasicService,ITransient
    {
        private readonly ISqlSugarClient _client;
        private readonly IUserBasicService _userBasicService;
        private readonly IAddressBookService _addressBookService;

        public OrderBasicService(ISqlSugarClient client, IUserBasicService userBasicService, IAddressBookService addressBookService)
        {
            _client = client;
            _userBasicService = userBasicService;
            _addressBookService = addressBookService;
        }

        /// <summary>
        /// 添加订单
        /// </summary>
        /// <param name="orderBasic"></param>
        /// <returns></returns>
        public bool InsertOrderBasic(OrderBasic orderBasic)
        {
            if (orderBasic == null) return false;
            //判断token中是否有用户数据
            int userId = JWTToken.GetUser().Id;
            if (userId <= 0) { return false; }
            orderBasic.UserId = userId;
            orderBasic.Number = "";
            orderBasic.OrderStatus = 1;
            orderBasic.CreateUser = userId;
            orderBasic.CreateDate = DateTime.Now;
            orderBasic.UpdateUser = userId;
            orderBasic.UpdateDate = DateTime.Now;
            orderBasic.IsDeleted = false;
            return _client.Insertable(orderBasic).ExecuteCommand() > 0;
        }

        /// <summary>
        /// 根据条件(订单号模糊查询，订单状态精确查询)分页查询
        /// </summary>
        /// <param name="searchOrderVo"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public PageResponseDto<List<OrderBasicDto>> SelectOrderPageByConditions(SearchOrderVo searchOrderVo, int pageIndex, int pageSize)
        {
            int size = 0;
            List<OrderBasicDto> orderBasicDtos = _client.Queryable<OrderBasic>()
                .WhereIF(!String.IsNullOrEmpty(searchOrderVo.Number), ob => ob.Number.Contains(searchOrderVo.Number))
                .WhereIF(searchOrderVo.OrderStatus != 0, ob => ob.OrderStatus == searchOrderVo.OrderStatus)
                .OrderBy(db => db.CreateDate, OrderByType.Desc)
                .ToPageList(pageIndex, pageSize, ref size)
                .Adapt<List<OrderBasicDto>>();
            for (int i = 0; i< orderBasicDtos.Count;i++)
            {
                orderBasicDtos[i] = this.SelectOrderById(orderBasicDtos[i].Id);
            }
            PageResponseDto<List<OrderBasicDto>> pageResponseDto = new PageResponseDto<List<OrderBasicDto>>();
            pageResponseDto.data = orderBasicDtos;
            pageResponseDto.size = size;
            return pageResponseDto;
        }

        /// <summary>
        /// 根据id修改订单状态
        /// </summary>
        /// <param name="orderBasic"></param>
        /// <returns></returns>
        public bool UpdateOrderBasicById(OrderBasic orderBasic)
        {
            if (orderBasic == null) return false;
            OrderBasic order = _client.Queryable<OrderBasic>()
                .Where(x => x.Id == orderBasic.Id)
                .First();
            order.OrderStatus = orderBasic.OrderStatus;
            return _client.Updateable(order).ExecuteCommand() > 0;
        }

        /// <summary>
        /// 根据id将数据库原始数据转化为详情数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public OrderBasicDto SelectOrderById(int id)
        {
            OrderBasic orderBasic = _client.Queryable<OrderBasic>()
                .Where(x => x.Id == id)
                .First();
            int userId = orderBasic.UserId;
            int addressId = orderBasic.AddressId;
            int CreateId = orderBasic.CreateUser;
            int UpdateId = orderBasic.UpdateUser;
            OrderBasicDto orderBasicDto = orderBasic.Adapt<OrderBasicDto>();
            orderBasicDto.userBasicDto = _userBasicService.SelectUserById(userId);
            orderBasicDto.addressBook = _addressBookService.SelectAddressBookById(addressId);
            orderBasicDto.CreateUser = _userBasicService.SelectUserById(CreateId).Username;
            orderBasicDto.UpdateUser = _userBasicService.SelectUserById(UpdateId).Username;
            return orderBasicDto;
        }
    }
}
